package com.video_study.service.serviceImpl;

import com.video_study.dao.CollectRecordMapper;
import com.video_study.dao.CourseMapper;
import com.video_study.dao.LookRecordMapper;
import com.video_study.dao.UsersMapper;
import com.video_study.dto.CourseDto2;
import com.video_study.pojo.LookRecord;
import com.video_study.pojo.Users;
import com.video_study.result.ExceptionMsg;
import com.video_study.result.ResponseData;
import com.video_study.service.UserService;
import com.video_study.util.Md5Util;
import com.video_study.util.UpUtils;
import com.video_study.util.pageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    CollectRecordMapper collectRecordMapper;
    @Autowired
    LookRecordMapper lookRecordMapper;
    @Autowired
    CourseMapper courseMapper;

    @Override
    public ResponseData login(String username, String password) {
        Users user = usersMapper.selectByUsername(username);
        if (user != null && user.getPassword().equalsIgnoreCase(Md5Util.getMd5(username, password))) {
            return new ResponseData(ExceptionMsg.SUCCESS);
        } else {
            return new ResponseData("000407", "用户名或者密码错误", null);
        }
    }
    @Override
    public ResponseData getReserves(String id) {
        List<CourseDto2> collectRecords = collectRecordMapper.getCoursesByUserId(id);
        return new ResponseData(collectRecords);
    }

    @Override
    @Transactional
    public ResponseData insertRecords(LookRecord lookRecord) {
        lookRecord.setId(UUID.randomUUID().toString());
        lookRecord.setLookTime(new Date());
        try {
            int insert = lookRecordMapper.insert(lookRecord);
            int result = courseMapper.addPlays(lookRecord.getCourseId());
        }catch(Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return new ResponseData(ExceptionMsg.SUCCESS);

    }

    @Override
    public ResponseData getRecords(String id) {
        List<CourseDto2> records = lookRecordMapper.getRecordsByUserId(id);
        return new ResponseData(records);
    }

    @Override
    public ResponseData getUserById(String id) {
        Users users = usersMapper.selectByPrimaryKey(id);
        users.setPassword(null);
        return new ResponseData(users);
    }

    @Override
    public ResponseData getUsers(Integer page, Integer limit, String id, String username, String phone) {
        pageUtil pageUtil = new pageUtil(page, limit);
        if (id == null && username == null && phone == null) {
            List<Users> users = usersMapper.getUsers(pageUtil.getPage(), pageUtil.getLimit());
            Long count = usersMapper.getCount();
            return new ResponseData(users, count);
        } else {
            List<Users> users = usersMapper.getUser(pageUtil.getPage(), pageUtil.getLimit(), id, username, phone);
            Long count = usersMapper.getCount1(pageUtil.getPage(), pageUtil.getLimit(), id, username, phone);
            return new ResponseData(users, count);
        }
    }

    @Override
    public ResponseData upImg(MultipartFile file, HttpServletRequest request) {
        Integer upfile = UpUtils.upfile(file, request);
        if (upfile == 1) {
            System.out.println(file.getOriginalFilename());
            return new ResponseData(ExceptionMsg.SUCCESS, file.getOriginalFilename());
        } else {
            return new ResponseData(ExceptionMsg.FAILD);
        }
    }

    @Override
    @Transactional
    public ResponseData addUser(Users users, HttpServletRequest request) {
        Users users1 = usersMapper.selectByUsername(users.getUsername());
        if(users1!=null)
            return new ResponseData(ExceptionMsg.FAILD);
        try {
            String id = UUID.randomUUID().toString();
            users.setId(id);
            users.setPassword(Md5Util.getMd5(users.getUsername(),users.getPassword()));
            if(users.getFaceImage()==null){
                users.setFaceImage("img/douyin.png");
            }
            if(users.getNickname()==null){
                users.setNickname(users.getUsername());
            }
            int count = usersMapper.insert(users);
            if (count > 0) {
                return new ResponseData(ExceptionMsg.SUCCESS);
            } else {
                return new ResponseData(ExceptionMsg.FAILD);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseData("000407", "未知异常");
        }
    }

    @Override
    @Transactional
    public ResponseData editUser(Users users) {
        try {
            Users users1 = usersMapper.selectByPrimaryKey(users.getId());
            //还需要加逻辑判定  -- 去学习正则表达式
            if (users1 != null) {
                Integer i = usersMapper.updateByPrimaryKey(users);
                if (i == 0) {
                    return new ResponseData(ExceptionMsg.FAILD);
                } else {
                    return new ResponseData(ExceptionMsg.SUCCESS);
                }
            } else {
                return new ResponseData("000407", "请刷新以后在尝试！");
            }

        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseData(ExceptionMsg.FAILD);
        }
    }

    @Override
    public ResponseData deleteById(String[] ids) {
        int j = 0;
        try {
            for (String id : ids) {
                Users users = usersMapper.selectByPrimaryKey(id);
                if (users != null) {
                    j = usersMapper.deleteByPrimaryKey(id);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return new ResponseData(ExceptionMsg.SUCCESS);
    }

    @Override
    public ResponseData register(String id,String username, String password) {
        Users user = usersMapper.insertByUserId(id,username,password);
        try {
           id = UUID.randomUUID().toString();
            user.setId(id);
            Users count = usersMapper.insertByUserId(id,username,password);
            if (count ==null) {
                return new ResponseData(ExceptionMsg.SUCCESS);
            } else {
                return new ResponseData(ExceptionMsg.FAILD);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseData("000407", "注册失败");
        }
    }

    @Override
    public ResponseData findpwd(Users users) {
        Users user = usersMapper.selectByUsername(users.getUsername());
        user.setPassword(Md5Util.getMd5(users.getUsername(),users.getPassword()));
        int i = usersMapper.updateByPrimaryKey(user);
        return new ResponseData(ExceptionMsg.SUCCESS);
    }
}